//
//  HomeViewController.swift
//  SHWeibo2.0
//
//  Created by 黄礼潮 on 16/2/21.
//  Copyright © 2016年 黄礼潮. All rights reserved.
//

import UIKit
import YYModel
import AFNetworking
import SVProgressHUD

private let CZHomeCell = " CZHomeCell"

private let CZTipLcCell = "CZTipLcCell"

class HomeViewController: RooterViewController {
    // MARK --- 定义一个私有属性
    private lazy var statusList = [StatusViewModel]()

    override func viewDidLoad() {
        super.viewDidLoad()
        //添加自定义Cell
        setupCell()
        
        visitorView?.setupInfo( title: "关注一些人，回这里看看有什么惊喜")
        
        //注册通知
        NSNotificationCenter.defaultCenter().addObserver(self, selector:"reachability", name: AFNetworkingReachabilityDidChangeNotification, object: nil)
    }
    
    private func setupCell(){
        
        
        //注册Cell
        tableView.registerClass(StatusCell.self, forCellReuseIdentifier: CZHomeCell)
        //注册提示Cell
        tableView.registerClass(CZTipCell.self, forCellReuseIdentifier:CZTipLcCell)
        
//        //设置预估行高
//        tableView.estimatedRowHeight = 200
//        //自动计算行高，会根据Cell的自动布局，动态的计算contantSize 中Cell 的height
//        tableView.rowHeight = UITableViewAutomaticDimension
        
        tableView.separatorStyle = .None
    }
    
    deinit{
        NSNotificationCenter.defaultCenter().removeObserver(self, name: AFNetworkingReachabilityDidChangeNotification, object: nil)
    
    }
     @objc  private func reachability(){
    
       self.tableView.reloadSections(NSIndexSet(index:0), withRowAnimation: .Automatic)
    }
    
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
        
        // 可能出现内存警告的问题，要在调用方法前，移除数据，刷新数据
        // 注意：Swift 中 lazy 的属性 ** 千万不要设置成 nil！！！
        if self.view.window == nil {
            // 1. 清理数组
            listViewModel.cleanUp()
            // 2. 刷新数据
            self.loadData()
        }

    }
    

    override func loadData() {

        //实例化上拉刷新视图
        let isPullUp = refreshControl.isPullupRefresh
        //显示菊花
        refreshControl.beginRefreshing()
        
        listViewModel.loadStatus(isPullUp) { (isSuccess) -> () in
            /// 结束刷新
            self.refreshControl.endRefreshing()
            if !isSuccess{
              
             SVProgressHUD.showInfoWithStatus("您的网络不给力")
            }
            /// 刷新表格
            self.tableView.reloadData()
        }
        
    }
    
    private lazy var listViewModel = StatusListViewModel()
}
//MARK ---UITableViewDataSurce
/// Redundant conformance of 'HomeViewController' to protocol 'UITableViewDataSource'  重复遵守协议了，基类已经继承了协议
extension HomeViewController{
    
    func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        
        return 2
    }

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        if section == 0 {
        //如果网络没有连接  就返回0
            return NetworkTools.shardTools.reachability ? 0 : 1
            
        }
          return  listViewModel.statusList.count
 
    }
    
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        
        if indexPath.section == 0 {
        
           return tableView.dequeueReusableCellWithIdentifier(CZTipLcCell, forIndexPath: indexPath)
        }
            let cell = tableView.dequeueReusableCellWithIdentifier(CZHomeCell, forIndexPath: indexPath) as! StatusCell
            
        
            //设置视图模型
            cell.viewModel = listViewModel.statusList[indexPath.row]
        
            return cell
        
    }
    
    func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
    
        if indexPath.section == 0{
            
            return 44
        }
    
        return listViewModel.statusList[indexPath.row].rowHeight
    }
    
    


}